package com.bdqn.zhang.service.impl;

import com.bdqn.zhang.dao.DeptMapper;
import com.bdqn.zhang.pojo.Dept;
import com.bdqn.zhang.service.DeptService;
import com.bdqn.zhang.utils.Convert;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;


/**
 * 部门 服务层实现
 * 
 * @author ruoyi
 * @date 2019-05-05
 */

public class DeptServiceImpl implements DeptService
{

	/**
     * 查询部门信息
     * 
     * @param deptId 部门ID
     * @return 部门信息
     */
    @Override
	public Dept selectDeptById(int deptId)
	{
		String resource = "mybatis-config.xml";
		Dept dept2 = null;
		SqlSession sqlSession = null;
		try{
			//1 获取mybatis-config.xml的输入流
			InputStream is = Resources.getResourceAsStream(resource);
			//2 创建SqlSessionFactory对象，完成对配置文件的读取
			SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
			//3 创建sqlSession
			sqlSession = factory.openSession();
			//4 调用mapper文件来对数据进行操作，必须先把mapper文件引入到mybatis-config.xml中
			dept2 = sqlSession.getMapper(DeptMapper.class).selectDeptById(deptId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dept2;
	}
	
	/**
     * 查询部门列表
     * 
     * @param dept 部门信息
     * @return 部门集合
     */
	@Override
	public List<Dept> selectDeptList(Dept dept)
	{

		return null;
	}
	
    /**
     * 新增部门
     * 
     * @param dept 部门信息
     * @return 结果
     */
	@Override
	public int insertDept(Dept dept)
	{
		String resource = "mybatis-config.xml";
		int count = 0;
		SqlSession sqlSession = null;
		try{
			//1 获取mybatis-config.xml的输入流
			InputStream is = Resources.getResourceAsStream(resource);
			//2 创建SqlSessionFactory对象，完成对配置文件的读取
			SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
			//3 创建sqlSession
			sqlSession = factory.openSession(false);
			//4 调用mapper文件来对数据进行操作，必须先把mapper文件引入到mybatis-config.xml中
			count = sqlSession.getMapper(DeptMapper.class).insertDept(dept);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}
	
	/**
     * 修改部门
     * 
     * @param dept 部门信息
     * @return 结果
     */
	@Override
	public int updateDept(Dept dept)
	{
		String resource = "mybatis-config.xml";
		int count = 0;
		SqlSession sqlSession = null;
		try{
			//1 获取mybatis-config.xml的输入流
			InputStream is = Resources.getResourceAsStream(resource);
			//2 创建SqlSessionFactory对象，完成对配置文件的读取
			SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
			//3 创建sqlSession
			sqlSession = factory.openSession(false);
			//4 调用mapper文件来对数据进行操作，必须先把mapper文件引入到mybatis-config.xml中
			count = sqlSession.getMapper(DeptMapper.class).updateDept(dept);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	/**
     * 删除部门对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteDeptByIds(String ids)
	{
		String resource = "mybatis-config.xml";
		int count = 0;
		SqlSession sqlSession = null;
		try{
			//1 获取mybatis-config.xml的输入流
			InputStream is = Resources.getResourceAsStream(resource);
			//2 创建SqlSessionFactory对象，完成对配置文件的读取
			SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
			//3 创建sqlSession
			sqlSession = factory.openSession(false);
			//4 调用mapper文件来对数据进行操作，必须先把mapper文件引入到mybatis-config.xml中
			count = sqlSession.getMapper(DeptMapper.class).deleteDeptByIds(Convert.toStrArray(ids));
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}
	@Override
	public int deleteDeptById(int deptId)
	{
		String resource = "mybatis-config.xml";
		int count = 0;
		SqlSession sqlSession = null;
		try{
			//1 获取mybatis-config.xml的输入流
			InputStream is = Resources.getResourceAsStream(resource);
			//2 创建SqlSessionFactory对象，完成对配置文件的读取
			SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
			//3 创建sqlSession
			sqlSession = factory.openSession(false);
			//4 调用mapper文件来对数据进行操作，必须先把mapper文件引入到mybatis-config.xml中
			count = sqlSession.getMapper(DeptMapper.class).deleteDeptById(deptId);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}
	
}
